# -*- coding: utf-8 -*-

from db.mysql import MysqlClient
import json
import execjs
from itertools import product
from common.helper import Helper
from common.config import *

class Arrangement(object):

    def __init__(self):
        self.mysql = MysqlClient()
        self.name = "安农之安行天下"

    # 生产数据
    def genProductInfo(self):

        plan = {

            "plan": [

                {

                    "airplane_death": "500000",
                    "train_death": "300000",
                    "ship_disability": "200000",
                    "passenger_death": "200000"
                },

            ],
            "days": ["1m", "6m", "12m"],
            "people_count":[str(i) for i in range(1,5)]

        }
        for v in product( plan["plan"],plan["days"],plan["people_count"]):


            price = Helper.decimalParse(self.calcPremium(int(v[1].replace("m","")),int(v[2])))

            data = {

                "name":self.name,
                "company":"安华农业保险",
                "days":v[1],
                "people_count":v[2],
                "price":price
            }
            data.update( v[0])
            self.mergeTable( data )
            # 合并数据表


    # 计算保费
    def calcPremium(self,months,count):

        js = '''
        function calcPremium(months,count) {
            var sum;
            if (months == 1) {
                sum = count * 30;
                return sum
            } else if (months == 6) {
                sum = count * 60;
                return sum
            } else if (months == 12) {
                sum = count * 100;
                return sum
            }
        }
        '''

        cxt = execjs.compile( js )

        result = cxt.call("calcPremium",months,count)

        return result


    # 合并表格
    def mergeTable(self, data):

        SQL = 'INSERT INTO `'+TABLE_ACCIDENT+'`(`name`,`company`,`days`,`people_count`,`price`,`airplane_death`,`train_death`,`passenger_death`,`ship_disability`)VALUES("{name}","{company}","{days}","{people_count}","{price}","{airplane_death}","{train_death}","{passenger_death}","{ship_disability}")'

        print( SQL.format(**data))

        self.mysql.cursor.execute(SQL.format(**data))
        self.mysql._conn.commit()


if __name__ == '__main__':

    a = Arrangement()
    a.genProductInfo()